import { store } from "@/store";
import { defineStore } from "pinia";
import { router } from "@/router";
import { useMultiTagsStoreHook } from "./multiTags";
import { PLATFORM_Map, usePageVariablesStoreHook } from "./pageVariables";
import { storageLocal } from "@pureadmin/utils";
import { userKey } from "@/utils/auth";


export const usePageReloadStore = defineStore({
  id: "page-reload",
  state: () => ({
    routerKeys: 0,
  }),
  actions: {
    reloadPages() {
      this.routerKeys++
      if (usePageVariablesStoreHook().getPlatform() == PLATFORM_Map.operate) {
        if (usePageVariablesStoreHook().operateProjectInfo?.expireStatus == 2) {
          router.push(`/error/expire`)
        } else if (storageLocal().getItem(userKey).userInfo?.isSuper == 2 && (usePageVariablesStoreHook().getOperate().checkedProject == null || usePageVariablesStoreHook().getOperate().checkedProject == 0)) {
          router.push(`/error/noProjectSelected`)
        } else if (['/error/expire', '/error/noProjectSelected'].includes(router.currentRoute.value.fullPath)) {
          router.push(`/`)
        }
      } else if (['/error/expire', '/error/noProjectSelected'].includes(router.currentRoute.value.fullPath)) {
        router.push(`/`)
      }
    }
  }
});

function usePageReloadStoreHook() {
  return usePageReloadStore(store);
}
export { usePageReloadStoreHook }